package cn.tedu.sims.controller;

import cn.tedu.sims.pojo.dto.NoticeAddNewDTO;
import cn.tedu.sims.pojo.vo.NoticeListItemVO;
import cn.tedu.sims.pojo.vo.NoticeStandardVO;
import cn.tedu.sims.service.NoticeService;
import cn.tedu.sims.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Api(tags = "公告管理")
@Validated
@Slf4j
@RestController
@RequestMapping("/notices")
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @ApiOperation("添加公告")
    @ApiOperationSupport(order = 200)
    @PreAuthorize("hasAuthority('/notices/insert')")
    @PostMapping("/insert")
    public JsonResult<Void> insert(@Valid NoticeAddNewDTO noticeAddNewDTO){
        System.out.println("noticeAddNewDTO = " + noticeAddNewDTO);
        noticeService.addNew(noticeAddNewDTO);
        return JsonResult.ok();
    }

    @ApiOperation("删除公告")
    @ApiOperationSupport(order = 300)
    @PreAuthorize("hasAuthority('/notice/id/delete')")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@PathVariable Long id){
        noticeService.deleteById(id);
        return JsonResult.ok();
    }

    @ApiOperation("批量删除公告")
    @ApiOperationSupport(order = 301)
    @PreAuthorize("hasAuthority('/notice/id/deleteBatch')")
    @PostMapping("/{list}/deleteBatch")
    public JsonResult<Void> deleteBatch(@PathVariable Long[] list){
        noticeService.deleteBatch(list);
        return JsonResult.ok();
    }

    @ApiOperation("查询公告")
    @ApiOperationSupport(order = 400)
    @PreAuthorize("hasAuthority('/notice')")
    @GetMapping("")
    public JsonResult<List<NoticeListItemVO>> list(){
        List<NoticeListItemVO> list = noticeService.list();
        return JsonResult.ok(list);
    }

    @ApiOperation("通过标题查询公告")
    @ApiOperationSupport(order = 401)
    @PreAuthorize("hasAuthority('/notice/selectByTitle')")
    @GetMapping("/selectByTitle")
    public JsonResult<List<NoticeListItemVO>> selectByTitle(String title){
        List<NoticeListItemVO> list=noticeService.selectByTitle(title);
        return JsonResult.ok(list);
    }

    @ApiOperation("通过ID查询公告详情")
    @ApiOperationSupport(order = 402)
    @PreAuthorize("hasAuthority('/notice/selectById')")
    @GetMapping("/selectById")
    public JsonResult<NoticeStandardVO> selectById(Long id){
        NoticeStandardVO standardVO=noticeService.selectById(id);
        return JsonResult.ok(standardVO);
    }
}
